<script>
$(document).ready(function() {
	var address;
	$('#state').trigger("change");
});
$('#cep').live('blur', function(){
		url = "/address/get-address?cep="+$(this).val(),
		
		$.get(url, function(data){
			address = data;
				if(data != null){
					$('#address').val(address.address_type+" "+address.address);

					$('#neighborhood').val(address.neighborhood);

					if (data != null) {
						$('#state option[value="' + address.state + '"]').attr({
							selected : "selected"
						});
					} else {
						$('#state option[value=""]').attr({
							selected : "selected"
						});
					}

					$('#state').trigger("change");

					if (data != null) {
						$('#city option[value="' + address.city + '"]').attr({
							selected : "selected"
						});
					} else {
						$('#city option[value=""]').attr({
							selected : "selected"
						});
					}
				}else{
					$('#address').val("");
					
					$('#neighborhood').val("");
	
					$('#state option[value=""]').attr({
						selected : "selected"
					});
					
					$('#city option[value=""]').attr({
						selected : "selected"
					});
				
					alert('CEP não encontrado!');
				}
			}, 'json')
})

	$('.type').live('click', function(){
		$('.type').removeClass('checked');
		$('.type').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#type').val($(this).prev().val());
	})
	
	$('.sex').live('click', function(){
		$('.sex').removeClass('checked');
		$('.sex').addClass('unchecked');
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#sex').val($(this).prev().val());
	})
	
	$('.term').live('click', function(){
		$(this).removeClass('unchecked');
		$(this).addClass('checked');
		$('#term').val($(this).prev().val());
	})

	$('#state').live('change', function(){
		url='/address/get-cities?state='+$(this).val();

		$.get(url, function(data){
			$('#city').html(data);
			if(address != null && address.address != undefined){
				$('#city option[value="' + address.city + '"]').attr({
					selected : "selected"
				});
			}else{
				$('#city option[value="'+$('#cityC').val()+'"]').attr({
					selected : "selected"
				});
			}
		});

	});
</script>


<form action="/client/client" method="post" id="myForm">
<br/>
<div id="person_data" style="height: 330px;">
	<div style="float: left;">
		<table >
		<tr>
			<td align="right">
				<span>Nome Completo *</span>
			</td>
			<td>
				<input type="text" class="text" name="name" id="name" size="65" title="Nome Completo" maxlength="65" <?php if(isset($this->errorMessages['name'])):?>placeholder="<?php echo $this->errorMessages['name']?>" <?php else :?>value="<?php echo $this->form['name']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Cpf *</span>
			</td>
			<td>
				<input type="text" class="text" name="cpf" id="cpf" onkeyup="maskCpf(this);" title="CPF" maxlength="16" <?php if(isset($this->errorMessages['cpf'])):?>placeholder="<?php echo $this->errorMessages['cpf']?>"<?php else :?>value="<?php echo $this->form['cpf']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Sexo *</span>
			</td>
			<td>	
			<input type="hidden" id="sex" name="sex" value="<?php echo $this->form['sex']?>">
			
			<input id="fem" value="Feminino" type="radio">
			<label id="fe" for="fem" class="sex <?php if($this->form['sex'] == "Feminino"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">F</label>
			
			<input id="man" value="Masculino" type="radio">
			<label id="ma" for="man" class="sex <?php if($this->form['sex'] == "Masculino"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">M</label>
			
			<?php if(isset($this->errorMessages['sex'])):?>
				<label style="font-size: 20px; margin-left: 20px; font-weight:bold;"><?php echo $this->errorMessages['sex']?></label>
			<?php endif;?>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Data de Nascimento *</span>
			</td>
			<td>
				<input type="text" class="text" name="birth" id="birth" onkeyup="maskDate(this);" <?php if(isset($this->errorMessages['birth'])):?>placeholder="<?php echo $this->errorMessages['birth']?>" <?php else :?>value="<?php echo $this->form['birth']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Telefone Preferencial *</span></td>
			<td>
			<input type="text" class="text" name="phone" id="phone" onkeyup="maskPhone(this);" <?php if(isset($this->errorMessages['phone'])):?>placeholder="<?php echo $this->errorMessages['phone']?>" <?php else :?>value="<?php echo $this->form['phone']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Celular</span>
			</td>
			<td>
				<input type="text" class="text" name="cel" id="cel" onkeyup="maskPhone(this);" <?php if(isset($this->errorMessages['cel'])):?>placeholder="<?php echo $this->errorMessages['cel']?>" <?php else :?>value="<?php echo $this->form['cel']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Login *</span>
			</td>
			<td>
				<input type="text" class="text" name="login" id="login" size="15" maxlength="15" <?php if(isset($this->errorMessages['login'])):?>placeholder="<?php echo $this->errorMessages['login']?>" <?php else :?>value="<?php echo $this->form['login']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Email *</span>
			</td>
			<td>
				<input type="text" class="text" name="email" id="email" size="30" maxlength="50" <?php if(isset($this->errorMessages['email'])):?>placeholder="<?php echo $this->errorMessages['email']?>" <?php else :?>value="<?php echo $this->form['email']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right">
				<span>Senha *</span>
			</td>
			<td>
				<input type="password" class="text" name="pss" id="pss" value="" size="15" maxlength="15" <?php if(isset($this->errorMessages['password'])):?>placeholder="<?php echo $this->errorMessages['password']?>"<?php endif;?>>
 			</td>
		</tr>
		
		<tr>
			<td align="right"><span>Confirma Senha *</span></td>
			<td><input type="password" class="text" name="pss2" id="pss2" size="15" maxlength="15" <?php if(isset($this->errorMessages['confirm'])):?>placeholder="<?php echo $this->errorMessages['confirm']?>"<?php endif;?>>
			</td>
		</tr>
		</table>
	</div>
	
	<div style="float: right; margin-right: 30px;">
		<table>
		<tr>
			<td align="right"><span>Cep *</span></td>
			<td><input type="text" class="text" name="cep" id="cep" title="Cep" maxlength="9" onkeyup="maskCep(this);" <?php if(isset($this->errorMessages['zipcode'])):?>placeholder="<?php echo $this->errorMessages['zipcode']?>" <?php else :?>value="<?php echo $this->address['zipcode']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Endereço *</span></td>
			<td><input type="text" class="text" name="address" id="address" <?php if(isset($this->errorMessages['address'])):?>placeholder="<?php echo $this->errorMessages['address']?>"<?php else :?>value="<?php echo $this->address['address']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Número *</span></td>
			<td><input type="text" class="text" name="num" id="num" <?php if(isset($this->errorMessages['number'])):?>placeholder="<?php echo $this->errorMessages['number']?>"<?php else :?>value="<?php echo $this->address['number']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Complemento</span></td>
			<td><input type="text" class="text" name="comp" id="comp" <?php if(isset($this->errorMessages['comp'])):?>placeholder="<?php echo $this->errorMessages['comp']?>" <?php else :?>value="<?php echo $this->address['comp']?>" <?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Tipo de moradia *</span></td>
			<td>
				<div style="float: left;">
					<input type="hidden" id="type" name="type" value="<?php echo $this->address['type']?>">
					<input id="ape" value="Apartamento" class="card-buttom" name="ship" class="formLeft" type="radio" >
					<label id="ap" for="ape" class="type <?php if($this->address['type'] == "Apartamento"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Apartamento</label><br/>
					<input id="cas" value="Casa" class="card-buttom" name="ship" class="formLeft" type="radio" >
					<label id="ca" for="cas" class="type <?php if($this->address['type'] == "Casa"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Casa</label><br/>
					<input id="com" value="Comercial" class="card-buttom" name="ship" class="formLeft" type="radio" >
					<label id="co" for="com" class="type <?php if($this->address['type'] == "Comercial"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Comercial</label><br/>
				</div>
				<?php if(isset($this->errorMessages['type'])):?>
					<div style="float: left; margin-top: 25px;">
						<label style="font-size: 20px; float: left; font-weight:bold;"><?php echo $this->errorMessages['type']?></label>
					</div>
				<?php endif;?>
			</td>
			
		</tr>
		<tr>
			<td align="right"><span>Referência</span></td>
			<td><input value="<?php echo $this->address['ref']?>" type="text" class="text" name="ref" id="ref" <?php if(isset($this->errorMessages['ref'])):?>placeholder="<?php echo $this->errorMessages['ref']?>"<?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Bairro *</span></td>
			<td><input value="<?php echo $this->address['neighborhood']?>" type="text" class="text" name="neighborhood" id="neighborhood" size="15" maxlength="15" <?php if(isset($this->errorMessages['neighborhood'])):?>placeholder="<?php echo $this->errorMessages['neighborhood']?>"<?php endif;?>>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Estato *</span></td>
			<td>
			<select name="state" id="state" style="float: left;">
				<option value="">Selecione</option>
				<option <?php if($this->address['state'] == 'AC'):?>selected="selected"<?php endif;?> value="AC">AC</option>
				<option <?php if($this->address['state'] == 'AL'):?>selected="selected"<?php endif;?> value="AL">AL</option>
				<option <?php if($this->address['state'] == 'AM'):?>selected="selected"<?php endif;?> value="AM">AM</option>
				<option <?php if($this->address['state'] == 'AP'):?>selected="selected"<?php endif;?> value="AP">AP</option>
				<option <?php if($this->address['state'] == 'BA'):?>selected="selected"<?php endif;?> value="BA">BA</option>
				<option <?php if($this->address['state'] == 'CE'):?>selected="selected"<?php endif;?> value="CE">CE</option>
				<option <?php if($this->address['state'] == 'DF'):?>selected="selected"<?php endif;?> value="DF">DF</option>
				<option <?php if($this->address['state'] == 'ES'):?>selected="selected"<?php endif;?> value="ES">ES</option>
				<option <?php if($this->address['state'] == 'GO'):?>selected="selected"<?php endif;?> value="GO">GO</option>
				<option <?php if($this->address['state'] == 'MA'):?>selected="selected"<?php endif;?> value="MA">MA</option>
				<option <?php if($this->address['state'] == 'MG'):?>selected="selected"<?php endif;?> value="MG">MG</option>
				<option <?php if($this->address['state'] == 'MS'):?>selected="selected"<?php endif;?> value="MS">MS</option>
				<option <?php if($this->address['state'] == 'MT'):?>selected="selected"<?php endif;?> value="MT">MT</option>
				<option <?php if($this->address['state'] == 'PA'):?>selected="selected"<?php endif;?> value="PA">PA</option>
				<option <?php if($this->address['state'] == 'PB'):?>selected="selected"<?php endif;?> value="PB">PB</option>
				<option <?php if($this->address['state'] == 'PE'):?>selected="selected"<?php endif;?> value="PE">PE</option>
				<option <?php if($this->address['state'] == 'PI'):?>selected="selected"<?php endif;?> value="PI">PI</option>
				<option <?php if($this->address['state'] == 'PR'):?>selected="selected"<?php endif;?> value="PR">PR</option>
				<option <?php if($this->address['state'] == 'RJ'):?>selected="selected"<?php endif;?> value="RJ">RJ</option>
				<option <?php if($this->address['state'] == 'RN'):?>selected="selected"<?php endif;?> value="RN">RN</option>
				<option <?php if($this->address['state'] == 'RO'):?>selected="selected"<?php endif;?> value="RO">RO</option>
				<option <?php if($this->address['state'] == 'RR'):?>selected="selected"<?php endif;?> value="RR">RR</option>
				<option <?php if($this->address['state'] == 'RS'):?>selected="selected"<?php endif;?> value="RS">RS</option>
				<option <?php if($this->address['state'] == 'SC'):?>selected="selected"<?php endif;?> value="SC">SC</option>
				<option <?php if($this->address['state'] == 'SE'):?>selected="selected"<?php endif;?> value="SE">SE</option>
				<option <?php if($this->address['state'] == 'SP'):?>selected="selected"<?php endif;?> value="SP">SP</option>
				<option <?php if($this->address['state'] == 'TO'):?>selected="selected"<?php endif;?> value="TO">TO</option>
		</select>
			<?php if(isset($this->errorMessages['state'])):?>
				<div style="float: left; ">
					<label style="font-size: 20px; float: left;"><?php echo $this->errorMessages['state']?></label>
				</div>
			<?php endif;?>
			</td>
		</tr>
		<tr>
			<td align="right"><span>Cidade *</span></td>
			<td>
			
			<input type="hidden" value="<?php echo $this->address['city']?>" id="cityC" name="cityC">
			<select name="city" id="city">
				<option value="">Selecione</option>
				<?php foreach ($this->cities as $city):?>
					<option <?php if($this->address['city'] == utf8_encode($city['des_city'])):?>selected="selected"<?php endif;?> value="<?php echo  utf8_encode($city['des_city'])?>"><?php echo  utf8_encode($city['des_city'])?></option>
				<?php endforeach;?>
			</select>
			</td>
		</tr>
		</table>
	</div>
</div>
	<div class="left">
		<div style="float: left; ">
			<input type="hidden" id="term" name="term" value="<?php if($this->term){echo $this->term;}else{echo 'reject';}?>">
			<input id="terms" value="accept" class="card-buttom" name="terms" class="formLeft" type="radio" >
			<label id="tm" for="terms" class="term <?php if($this->term == "accept"):?>checked<?php else:?>unchecked<?php endif;?>" style="padding: 0 0 0 18px;">Lí e Concordo com os <a href="#" style="color: blue" target="blank">Termos de Uso</a></label><br/>
		</div>
		<?php if(isset($this->error)):?>
			<br /><div style="float: left; ">
				<label style="font-size: 20px; float: left; font-weight: bold;"><?php echo $this->error?></label>
			</div>
		<?php endif;?>
	</div>
	<div class="form-buttons">
		<input type="submit" name="cadastrar" value="Cadastrar">
		<input type="reset" value="Limpar">
	</div>
</form>

